package com.inossem.warehousestorage_inossem.adapter.receive;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveDeliveryOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveReceiveDetailActivity;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveReceiveItemActivity;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveDeleteReceiveBean;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveDeliveryOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.receive.ReceiveDeleteReceiveRequest;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class ReceiveListAdapter extends BaseAdapter {

    private static final String CODE_NO = "0";//否

    private static final String CODE_YES = "1";//是

    private static final String CODE_DRAFT = "0";//草稿

    private static final String CODE_NOT_RECEIVE = "1";//未验收

    private static final String CODE_NOT_PASS = "2";//未通过

    private static final String CODE_RECEIVING = "3";//验收中

    private static final String CODE_DONE = "4";//已完成

    private Context mContext;
    private List<ReceiveDeliveryOverviewBean.DataBean.ReceiveListBean> mReceiveList;
    private LayoutInflater inflater;
    private String mDeliveryNumber, mSupplier, mContract, mPurchase, mSupplierContacts, mDirectSupply;
    private ReceiveDeliveryOverviewActivity.ReceiveDeliveryOverviewListener mReceiveDeliveryOverviewListener;

    public ReceiveListAdapter(Context context, ReceiveDeliveryOverviewBean.DataBean data, List<ReceiveDeliveryOverviewBean.DataBean.ReceiveListBean> receiveList, ReceiveDeliveryOverviewActivity.ReceiveDeliveryOverviewListener receiveDeliveryOverviewListener) {
        mContext = context;
        mDeliveryNumber = data.getDeliveryNumber();
        mSupplier = data.getSupplier();
        mContract = data.getContract();
        mPurchase = data.getPurchaseNumber() + " - " + data.getPurchaseType();
        mSupplierContacts = data.getSupplierContactsTel() + " - " + data.getSupplierContactsName();
        mDirectSupply = formatDirectSupply(context, data.getDirectSupply());
        mReceiveList = receiveList;
        mReceiveDeliveryOverviewListener = receiveDeliveryOverviewListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mReceiveList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mReceiveList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_receive_list_first_item, null);
            TextView deliveryNumber = (TextView) convertView.findViewById(R.id.deliveryNumber);
            TextView supplier = (TextView) convertView.findViewById(R.id.supplier);
            TextView contract = (TextView) convertView.findViewById(R.id.contract);
            TextView purchase = (TextView) convertView.findViewById(R.id.purchase);
            TextView supplierContacts = (TextView) convertView.findViewById(R.id.supplierContacts);
            TextView directSupply = (TextView) convertView.findViewById(R.id.directSupply);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            deliveryNumber.setText(mDeliveryNumber);
            supplier.setText(mSupplier);
            contract.setText(mContract);
            purchase.setText(mPurchase);
            supplierContacts.setText(mSupplierContacts);
            directSupply.setText(mDirectSupply);
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
        } else {
            ReceiveDeliveryOverviewBean.DataBean.ReceiveListBean receiveItem = mReceiveList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_receive_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, receiveItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.status = (TextView) view.findViewById(R.id.status);
        viewHolder.receiveNumber = (TextView) view.findViewById(R.id.receiveNumber);
        viewHolder.receiveDate = (TextView) view.findViewById(R.id.receiveDate);
        viewHolder.receiveCreator = (TextView) view.findViewById(R.id.receiveCreator);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ReceiveDeliveryOverviewBean.DataBean.ReceiveListBean receiveItem) {
        String status = receiveItem.getStatus();
        String statusMessage = "";
        switch (status) {
            case CODE_DRAFT:
                statusMessage += context.getResources().getString(R.string.activity_receive_delivery_overview_draft);
                break;
            case CODE_NOT_RECEIVE:
                statusMessage += context.getResources().getString(R.string.activity_receive_delivery_overview_not_receive);
                break;
            case CODE_NOT_PASS:
                statusMessage += context.getResources().getString(R.string.activity_receive_delivery_overview_not_pass);
                break;
            case CODE_RECEIVING:
                statusMessage += context.getResources().getString(R.string.activity_receive_delivery_overview_receiving);
                break;
            case CODE_DONE:
                statusMessage += context.getResources().getString(R.string.activity_receive_delivery_overview_done);
                break;
        }
        viewHolder.status.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_manage_not_receive));
        viewHolder.status.setText(statusMessage);
        viewHolder.receiveNumber.setText(receiveItem.getReceiveNumber());
        viewHolder.receiveDate.setText(receiveItem.getReceiveDate());
        viewHolder.receiveCreator.setText(receiveItem.getReceiveCreator());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                //草稿，未通过状态可以修改，未验收，验收中，已完成状态只能查看
                if (CODE_DRAFT.equals(status) || CODE_NOT_PASS.equals(status)) {
                    Intent intent = new Intent(context, ReceiveReceiveItemActivity.class);
                    intent.putExtra(ReceiveExtra.DELIVERY_NUMBER, mDeliveryNumber);
                    intent.putExtra(ReceiveExtra.RECEIVE_NUMBER, receiveItem.getReceiveNumber());
                    context.startActivity(intent);
                } else if (CODE_NOT_RECEIVE.equals(status) || CODE_RECEIVING.equals(status) || CODE_DONE.equals(status)) {
                    Intent intent = new Intent(context, ReceiveReceiveDetailActivity.class);
                    intent.putExtra(ReceiveExtra.DELIVERY_NUMBER, mDeliveryNumber);
                    intent.putExtra(ReceiveExtra.RECEIVE_NUMBER, receiveItem.getReceiveNumber());
                    context.startActivity(intent);
                }
            }
        });
        view.setOnLongClickListener((View v) -> {
            //草稿，未验收状态可以删除，未通过，验收中，已完成状态不可以删除
            if (CODE_DRAFT.equals(status) || CODE_NOT_RECEIVE.equals(status)) {
                Dialog dialog = DialogUtils.getConfirmDialog(context, "确认要删除验收项(" + receiveItem.getReceiveNumber() + ")", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        ReceiveDeleteReceiveRequest.request(context, receiveItem.getReceiveNumber(), new GsonBeanListener<ReceiveDeleteReceiveBean>() {
                            @Override
                            public void onSuccess(ReceiveDeleteReceiveBean bean) {
                                mReceiveDeliveryOverviewListener.onRequestSuccess(bean, dialog);
                            }
                        });
                    }
                });
                dialog.show();
            } else if (CODE_NOT_PASS.equals(status) || CODE_RECEIVING.equals(status) || CODE_DONE.equals(status)) {
                Utils.middleToast(context, "只有草稿和未验收状态可以删除");
            }
            return true;
        });
    }

    private class ViewHolder {
        private TextView status, receiveNumber, receiveDate, receiveCreator;
    }

    private String formatDirectSupply(Context context, String code) {
        String directSupply = "";
        switch (code) {
            case CODE_NO:
                directSupply = context.getResources().getString(R.string.activity_receive_delivery_overview_no);
                break;

            case CODE_YES:
                directSupply = context.getResources().getString(R.string.activity_receive_delivery_overview_yes);
                break;
        }
        return directSupply;
    }
}
